#!/usr/bin/env python

import requests,threading,time,json,zlib,gnupg,socket,psutil,os,sys,pymysql,queue,numpy,apsw
from flask import Flask
import libproxysql as ps

app = Flask("proxysql")

p = HttpParser()
gpg=gnupg.GPG()

pathname = os.path.dirname(sys.argv[0])        
abspath=os.path.abspath(pathname)
configfile=abspath+"/config.json"
try:
	cf=open(configfile,"r")
except:
	cf=open(configfile+".template","r")

log_conf=json.load(cf)
cf.close()

parameter={"device":socket.gethostname(),"allowed_ip":{"127.0.0.1":"25A4CF79414F10FD"},"gpg_keyid":"25A4CF79414F10FD"}
for n in parameter:
  if n in log_conf:
    parameter[n]=log_conf[n]


@app.route('/')
def hello_world():
    return 'Hello World!'


# since we're using threads, shouldn't we be able to pause execution of one?
@app.route('/slow')
def slow():
    import time
    time.sleep(10)
    return 'zzz'



if __name__ == "__main__":
    
    q=Queue(maxsize=0)
    
    sql_worker=threading.Thread(target=ps.sql_insert,args=(q,))
    sql_worker.setDaemon(True)
    sql_worker.start()

    with open('./log', 'a+') as log:
        try:
            app.run(threaded=True)
            log.write("done adding wsgi app\n")
        except Exception, e:
            log.write(repr(e))
        
        
